/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.flowable.app.rest.runtime;

import cmbchina.util.AjaxJson;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.flowable.app.model.runtime.CreateProcessInstanceRepresentation;
import org.flowable.app.model.runtime.ProcessInstanceRepresentation;
import org.flowable.app.service.runtime.FlowableProcessInstanceService;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.impl.RepositoryServiceImpl;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.rest.exception.FlowableConflictException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
@RequestMapping("/rest/cus-stop-process")
public class CusStopProcessInstancesResource {

    @Autowired
    protected FlowableProcessInstanceService processInstanceService;

    @Autowired
    protected RuntimeService runtimeService;

    @Autowired
    protected RepositoryServiceImpl repositoryService;
    @RequestMapping(value = "/{processInstId}", method = RequestMethod.POST, produces = "application/json")
    public AjaxJson completeTaskForm(@PathVariable String processInstId, @RequestBody java.util.Map<String,Object> stringObjectMap) {
        AjaxJson ajaxJson = new AjaxJson();
//        startRequest.setName("zm-测试任务名称");
        ProcessInstance  processInstance =  runtimeService.createProcessInstanceQuery().processInstanceId(processInstId).singleResult();
        if (processInstance.isSuspended()) {
            throw new FlowableConflictException("Process instance with id '" + processInstance.getId() + "' is already suspended.");
        }
        runtimeService.suspendProcessInstanceById(processInstance.getId());
        return ajaxJson;
    }

    @RequestMapping(value = "/testRest", method = RequestMethod.GET, produces = "application/json")
    public AjaxJson testRest(@PathVariable String processInstId, @RequestBody java.util.Map<String,Object> stringObjectMap) {
        AjaxJson ajaxJson = new AjaxJson();
//        startRequest.setName("zm-测试任务名称");
        ProcessInstance  processInstance =  runtimeService.createProcessInstanceQuery().processInstanceId(processInstId).singleResult();
        if (processInstance.isSuspended()) {
            throw new FlowableConflictException("Process instance with id '" + processInstance.getId() + "' is already suspended.");
        }
        runtimeService.suspendProcessInstanceById(processInstance.getId());
        return ajaxJson;
    }
}
